package com.lxz.jucdemo.lock;

/**
 * @Author: 刘新智
 * @Date: 2025/10/25 11:30
 * @Description: synchronized 关键字解决线程安全问题
 * @Version: v1.0
 */
public class SynchronizedTest {
    public static void main(String[] args) {
        ticket t = new ticket();
        new Thread(()->{
            for (int i = 0; i < 15; i++) {
                t.sale();
            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 15; i++) {
                t.sale();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 15; i++) {
                t.sale();
            }
        },"C").start();

    }
}
class ticket{
    private int number = 30;
    public synchronized void sale(){
        if(number > 0){
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票，剩余：" + number);
        }
    }
}
